﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    public class PicturesController : ApiController
    {
        PicturesBO _PicturesBO = new PicturesBO();
        #region 图片信息 全部记录
        [HttpGet]
        public IHttpActionResult GetPicturesList()
        {
            try
            {
                var result = new PicturesBO().GetPicturesList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 图片信息 单条记录
        [HttpPost]
        public IHttpActionResult GetPicturesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int PictureID = model.PictureID ?? 0;
            if (PictureID == 0)
            {
                return Json(new Result(ResultCode.Fail, "图片ID 不能为空"));
            }
            try
            {
                var result = _PicturesBO.GetPicturesInfo(PictureID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 图片信息 添加
        [HttpPost]
        public IHttpActionResult InsertPicturesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string TouristID = model.TouristID ?? "";
            string PictureURL = model.PictureURL ?? "";
            string PictureDescription = model.PictureDescription ?? "";
            string UploadDate = model.UploadDate ?? "";
            if (string.IsNullOrEmpty(TouristID))
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(PictureURL))
            {
                return Json(new Result(ResultCode.Fail, "图片url不能为空"));
            }
            if (string.IsNullOrEmpty(PictureDescription))
            {
                return Json(new Result(ResultCode.Fail, "图片描述不能为空"));
            }
            try
            {
                var result = _PicturesBO.InsertPicturesInfo(model);
                return result ? Json(new Result(ResultCode.Success, "添加成功")) :
                Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 图片信息 修改
        [HttpPost]
        public IHttpActionResult UpdatePicturesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int PictureID = model.PictureID ?? 0;
            string TouristID = model.TouristID ?? "";
            string PictureURL = model.PictureURL ?? "";
            string PictureDescription = model.PictureDescription ?? "";
            string UploadDate = model.UploadDate ?? "";
            if (PictureID == 0)
            {
                return Json(new Result(ResultCode.Fail, "管理员ID不能为空"));
            }
            if (string.IsNullOrEmpty(TouristID))
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(PictureURL))
            {
                return Json(new Result(ResultCode.Fail, "图片url不能为空"));
            }
            if (string.IsNullOrEmpty(PictureDescription))
            {
                return Json(new Result(ResultCode.Fail, "图片描述不能为空"));
            }
            try
            {
                var result = _PicturesBO.UpdatePicturesInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) :
                Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 图片信息 删除
        [HttpPost]
        public IHttpActionResult DeletePicturesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int PictureID = model.PictureID ?? 0;
            if (PictureID == 0)
            {
                return Json(new Result(ResultCode.Fail, "图片ID 不能为空"));
            }
            try
            {
                var result = _PicturesBO.DeletePicturesInfo(PictureID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
    }
}
